/**
* @program: Go
*
* @description:配置文件
*
* @author: Mr.chen
*
* @create: 2020-08-19 15:01
**/
package config

import (
	"github.com/spf13/viper"
	"os"
	"path/filepath"
	"sync"
)

var once sync.Once
var realPath string
var Conf *Config
var (
	ApiAuthConfig = map[string]map[string]string{

		// 调用方
		"ydd-im": {
			"md5": "IgkibX71IEf382PT",
			"aes": "IgkibX71IEf382PT",
			"rsa": "rsa/public.pem",
		},
	}
)

const (
	AppName               = "ydd-im"
	SuccessReplyCode      = 0
	FailReplyCode         = 1
	SuccessReplyMsg       = "success"
	QueueName             = "shop_sub"
	RedisBaseValidTime    = 86400
	RedisPrefix           = "shop_server_"
	RedisRoomPrefix       = "shop_server_room_"
	RedisRoomOnlinePrefix = "shop_server_online_count_"
	MsgVersion            = 1
	OpSingleSend          = 2 // single user
	OpRoomSend            = 3 // send to room
	OpRoomCountSend       = 4 // get online user count
	OpRoomInfoSend        = 5 // send info to room
	AppReadTimeout        = 120
	AppWriteTimeout       = 120

	// 签名超时时间
	AppSignExpiry = "120000"

	// RSA Private File
	AppRsaPrivateFile = "rsa/private.pem"
	// 日志文件
	AppAccessLogName = "log/" + AppName + "-access.log"
	AppErrorLogName  = "log/" + AppName + "-error.log"
	AppGrpcLogName   = "log/" + AppName + "-grpc.log"

	// 系统告警邮箱信息
	SystemEmailUser = "935859473@qq.com"
	SystemEmailPass = "aiygjjarjmfxbfcb" //密码或授权码
	SystemEmailHost = "smtp.qq.com"
	SystemEmailPort = 465

	// 告警接收人
	ErrorNotifyUser = "935859473@qq.com"

	// 告警开关 1=开通 -1=关闭
	ErrorNotifyOpen = -1

	// Jaeger 配置信息
	JaegerHostPort = "127.0.0.1:6831"

	// Jaeger 配置开关 1=开通 -1=关闭
	JaegerOpen = 1
)

// 所有配置
type Config struct {
	ApiConfig     ApiConfig
	DbproxyConfig DbproxyConfig
	AccountConfig AccountConfig
	GroupConfig 	GroupConfig
}

func init() {
	Init()
}

// 初始化化配置
func Init() {
	once.Do(func() { // 只执行一次配置
		env := GetMode()
		realPath, _ := filepath.Abs("../../")
		configFilePath := realPath + "/config/" + env + "/"
		viper.SetConfigType("toml")
		viper.SetConfigName("/api")
		//viper.SetConfigName("/dbproxy")
		viper.AddConfigPath(configFilePath)
		err := viper.ReadInConfig()
		if err != nil {
			panic(err)
		}
		viper.SetConfigName("/dbproxy")
		err = viper.MergeInConfig()
		if err != nil {
			panic(err)
		}
		viper.SetConfigName("/account")
		err = viper.MergeInConfig()
		if err != nil {
			panic(err)
		}
		viper.SetConfigName("/group")
		err = viper.MergeInConfig()
		if err != nil {
			panic(err)
		}
		Conf = new(Config)
		viper.Unmarshal(&Conf.ApiConfig)
		viper.Unmarshal(&Conf.DbproxyConfig)
		viper.Unmarshal(&Conf.AccountConfig)
		viper.Unmarshal(&Conf.GroupConfig)
		//fmt.Println(GetDbConf(viper))
		Conf.ApiConfig.ApiDbconfig = GetApiDbConf()
		Conf.DbproxyConfig.DbproxyDbconfig = GetDbproxyDbConf()
	})
}

// 根据环境变量参数获取配置信息
func GetMode() string {
	env := os.Getenv("RUN_MODE")
	if env == "" {
		env = "dev"
	}
	return env
}

// 获取gin运行类型
func GetGinRunMode() string {
	env := GetMode()
	//gin have debug,test,release mode
	if env == "dev" {
		return "debug"
	}
	if env == "test" {
		return "debug"
	}
	if env == "prod" {
		return "release"
	}
	return "release"
}
// 获取api信息
func GetService() ApiBase {
	return Conf.ApiConfig.ApiBase
}

// 获取api配置信息
func GetApiConfig() ApiConfig {
	return Conf.ApiConfig
}

// 获取dbproxy配置信息
func GetDbProxyConfig() DbproxyConfig {
	return Conf.DbproxyConfig
}
// 获取account配置信息
func GetAccountConfig() AccountConfig {
	return Conf.AccountConfig
}
// 获取api日记信息
func GetZapLog() *LogConfig {
	return &Conf.ApiConfig.ApiZaplog
}
// 获取dbproxy日记信息
func GetDbZapLog() *DbproxyLogConfig {
	return &Conf.DbproxyConfig.DbproxyZaplog
}

// 获取dbproxy日记信息
func GetAccountZapLog() *AccountLogConfig {
	return &Conf.AccountConfig.AccountLogConfig
}
// 获取group配置信息
func GetGroupConfig() GroupConfig {
	return Conf.GroupConfig
}

// 获取group日记信息
func GetGroupZapLog() *GroupLogConfig {
	return &Conf.GroupConfig.GroupLogConfig
}